<template>
  <view>
    <!-- 头部搜索区域 -->
    <my-search @myclick="gotoSearch"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧滑动区域 -->
      <scroll-view  class="left-scroll-view" scroll-y="true"  :style="{height: wh + 'px'}">
        <block v-for="(item,id) in cateList" :key="id">
           <view :class="['left-scroll-view-item', id === active ? 'active': '']" @click="activeChange(id)">{{ item.cat_name }}</view>
        </block>
       
       
      </scroll-view>
      <!-- 右侧滑动区域 -->
      <scroll-view calss="right-scroll-view"  :scroll-top="scrollTop"  scroll-y="true" :style="{height: wh + 'px'}">
         <view class="cate-lv2" v-for="(item2, id2) in cateLevel2" :key="id2">
            <view class="cate-lv2-title">/{{ item2.cat_name }}/</view>
            <!-- 动态渲染三级分类的列表数据 -->
            
             <view class="cate-lv3-list">
                 <!-- 三级分类 Item 项 -->
                  <view class="cate-lv3-item" v-for="(item3, id3) in item2.children" :key="id3" @click="gotoGoodList(item3)">
                      <!-- 图片 -->
                       <image  class="img" :src="item3.cat_icon" mode=""></image>
                       <!-- 文字 -->
                      <text class="txt"> {{ item3.cat_name }}</text>
                  </view>
             </view>
         </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import { getCateList } from '@/api/cate.js'
  export default {
    data() {
      return {
        // 当前设备可用的高度
        wh: 0,
        // 分类数据列表
        cateList: [],
        // 二级分类列表
        cateLevel2: [],
        // 激活项
        active: 0,
        // 滚动条距离顶部的距离
        scrollTop: 0
      };
    },
    onLoad() {
      // 通过API文档获取当前设备可用高度
    const sysInfo = uni.getSystemInfoSync()
    // console.log(sysInfo);
    this.wh = sysInfo.windowHeight,
    
    // 商品分类
    this.getCate()
    },
      
    methods: {
    async  getCate() {
        const res = await getCateList()
        //console.log(res);
        this.cateList = res.message
        
        // 为二级分类赋值
        this.cateLevel2 = res.message[0].children
      },
      // 切换active值
      activeChange(id) {
        this.active = id
        
     // 切换重新为二级分类赋值
        this.cateLevel2 = this.cateList[id].children
        
       this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      
      //  跳转具体goods项
      gotoGoodList(item3) {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_pid
        })
      },
      // 搜索框按钮
      gotoSearch() {
        uni.navigateTo({
          url:"/subpkg/goods-search/goods-search"
        })
      }
    }
    
  }
</script>

<style lang="scss">
.scroll-view-container {
  display: flex;
  
  .left-scroll-view {
    width: 120px;
    
    .left-scroll-view-item {
      background-color: #f7f7f7;
      text-align: center;
      line-height: 60px;
      font-size: 12px;
      &.active{
        background-color: #fff;
        position: relative;
        // 此处的CSS很重要
        &::before {
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    }
  }
  
  .cate-lv2-title {
    font-size: 12px;
    font-weight: bold;
    text-align: center;
    padding: 15px 0;
  }
  
  .cate-lv3-list {
     display: flex;
     flex-wrap: wrap;
    .cate-lv3-item {
       width: 33.33%;
       display: flex;
       flex-direction: column;
       justify-content: center;
       align-items: center;
       margin-bottom: 10px;
      .img{
        width: 60px;
        height: 60px;
      }
      .txt {
        font-size: 12px
      }
    }
  }
}
</style>
